package com.miyake.loveviewweb.client.viewer.table;

import java.util.List;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.shared.model.DataColumnModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.client.viewer.table.ContentGetter.DataModelHandler;

public class ColumnTable extends Composite implements ITable {
    private DataModel dataModel;

    public ColumnTable(DataModel dataModel) {
        this.dataModel = dataModel;
        final Grid grid = new HeaderGrid(dataModel);
        initWidget(grid);
        
//        grid.resizeRows(10);
        
        new ContentGetter(dataModel, new DataModelHandler() {

            public void onUpdate(DataModel dataModel) {
                updateCopyGrid(dataModel, grid);
            }
            
        });
    }
    
    private void updateCopyGrid(DataModel dataModel, Grid grid) {
        int col = 0;
        //grid.resize(2, dataModel.getDataColumn().size());
        grid.resizeRows(2);
        for (DataColumnModel column : dataModel.getXyDataBody().getDataColumn()) {
            //grid.resizeRows(column.getValue().length);
            grid.setWidget(0, col, new HTML(column.getHeader()));
            
            TextArea textArea = new TextArea();
            textArea.setHeight("150px");
            textArea.setWidth("100px");
            grid.setWidget(1, col, textArea);
            String text = "";
            for (String s : column.getValue()) {
                text += s + "\n";
            }
            textArea.setText(text);

            col++;
        }
    }

    public Widget getUiWidget() {
        return this;
    }

    public List<DataColumnModel> getDataColumnList() {
        return dataModel.getXyDataBody().getDataColumn();
    }
}
